/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var dataReady;
(function(win){
    var doc = win.document,
    nav = win.navigator,
    loc = win.location;
    var leftBlockData = {
        contClass:'hfashLeftBlockContent',
        loadImgUrl:'http://www.happyfashion.com.au/images/loaderImg.gif',
        loadImgClass:'loaderImg'
    };
    var engImgData = {
        relObjId:'hyMainWrapper',
        engImgId:'hyEnlargeImg'
    }
    
    var ProductId = 'iPhone 4S 4GS 4 4G Genuine Leather Wallet Credit Flip Case Cover';
    var ProductType = 'iphonecase';
    
    var setupPage=function(){
        e$.addEventListener('ajaxbridgeready',whenBridgeReady);
    };
    var whenBridgeReady=function(){
        var bridge = e$.getFlash('hyEbayAjaxBridge');
        bridge.ajax('http://www.happyfashion.com.au/externalfile/php_ebayhelper.php','dataReady');
    };
    dataReady=function(data){
        e$.getByClass('hfashContentLeft')[0].removeChild(e$.getByClass('hyLoaderImg',e$.getByClass('hfashContentLeft')[0])[0]);
        for(var i=0; i<data.data.length;i++){
            makeFolderBlock(data.data[i]);
        }
        activeLeftBlock();
        var imgs = e$.getByTag('img', e$.getByClass('hfashContentLeft')[0]);
        e$.enlargImg(imgs, relObj, engImg, 3);
    }
    var activeLeftBlock = function(){
        var blocks = e$.getByClass('hfashLeftBlockHeader');
        var sildeUp = function(){
            var elem = e$.firstElem(e$.nextElem(this));
            this.hyheight = e$.firstElem(e$.nextElem(this)).offsetHeight;
            e$.animation(e$.nextElem(this), {
                height:0
            },300,function(elem){
                var head = e$.prevElem(elem);
                e$.setStyle(head, 'borderColor', '#6D3F1E');
                e$.setStyle(e$.firstElem(head), 'backgroundColor', '#6D3F1E');
                head.onclick = sildeDown;
            });
        }
        var sildeDown = function(){
            e$.animation(e$.nextElem(this), {
                height:this.hyheight
            },300,function(elem){
                var head = e$.prevElem(elem);
                e$.setStyle(head, 'borderColor', '#BD8F3E');
                e$.setStyle(e$.firstElem(head), 'backgroundColor', '#BD8F3E');
                head.onclick = sildeUp;
            });
        }
        for(var i=0; i<blocks.length; i++){
            blocks[i].onclick = sildeUp;
        }
    }
    var makeProductBlock = function(data){
        var outer = e$.makeElem('div');
        var img = new Image();
        var name = e$.makeElem('a');
        var price = e$.makeElem('span');
        img.src = data.img;
        name.title = data.name;
        name.href = data.url;
        if(data.name.length>=30){
            name.innerHTML = data.name.substring(0,30)+'...';
        }else{
            name.innerHTML = data.name;
        }
        price.innerHTML = data.price;
        outer.appendChild(img);
        outer.appendChild(e$.makeElem('br'));
        outer.appendChild(name);
        outer.appendChild(price);
        return outer;
    }
    var makeFolderBlock = function(data){
        var outer = e$.makeElem('div');
        e$.addClassName(outer, 'hfashLeftBlockWrapper');
        var header = e$.makeElem('div');
        e$.addClassName(header, 'hfashLeftBlockHeader');
        var headerText = e$.makeElem('span');
        e$.addClassName(headerText, 'topLeftCorner');
        headerText.innerHTML = data.categoryName;
        header.appendChild(headerText);
        outer.appendChild(header);
        var cWrapper = e$.makeElem('div');
        e$.addClassName(cWrapper, 'hfashLeftBlockContentWrapper');
        var cont = e$.makeElem('div');
        e$.addClassName(cont, 'hfashLeftBlockContent');
        cWrapper.appendChild(cont);
        outer.appendChild(cWrapper);
        var items = data.items;
        for(var i=0; i<items.length; i++){
            cont.appendChild(makeProductBlock(items[i])) 
        }
        var parent = e$.getByClass('hfashContentLeft')[0];
        parent.appendChild(outer);
    }
    var relObj=e$.getById(engImgData.relObjId),
    engImg=e$.getById(engImgData.engImgId);
    var addImgWrapper = e$.getByClass('hfashAdditionalImgWrapper')[0];
    var imgs = e$.getByTag('img',addImgWrapper);
    e$.enlargImg(imgs,relObj,engImg,2.5);
    var avail = e$.getById('hyAvailableChoices');
    if(avail !=null){
        imgs = e$.getByTag('img',avail);
        e$.enlargImg(imgs,relObj,engImg,2.8);
    }
    e$.JSReady = true;
    setupPage();
}(window))
